const app = getApp();

Page({
  data: {
    postId: '',
    amount: 0,
    postTitle: '宠物寄养服务',
    paymentMethod: 'wechat'
  },

  onLoad(options) {
    this.setData({
      postId: options.postId,
      amount: options.amount
    });
  },

  onMethodChange(e) {
    this.setData({ paymentMethod: e.detail.value });
  },

  async onPay() {
    try {
      const result = await app.request('/payments', 'POST', {
        postId: this.data.postId,
        amount: this.data.amount,
        paymentMethod: this.data.paymentMethod
      });

      wx.showToast({ 
        title: '支付成功', 
        icon: 'success',
        success: () => {
          setTimeout(() => {
            wx.navigateBack();
          }, 1500);
        }
      });
    } catch (error) {
      wx.showToast({ title: '支付失败', icon: 'none' });
    }
  }
});